package cn.biplam.back.bean.ddl.sql2db;

import cn.biplam.back.sql.type.Type;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

public class TypeParser {
	
	/**数据类型对象*/
	private Type mType;
	/**数据源信息*/
	private String dbType;
	
	/**
	 * 功能：构造方法
	 * @param mType 数据类型对象
	 * @param mDbResource 数据源信息
	 */
	public TypeParser(Type mType,String dbType){
		this.dbType = dbType;
		this.mType = mType;
	}
	
	/**
	 * 功能   取数据类型字符串
	 * @return
	 * @throws ErrorMessage
	 */
	public String get() throws ErrorMessage{
		
		/**规则字符串*/
		String sRule = null;
		
		/**设置KvMap参数列表，key值要和规则里的参数名一直*/
		KvMap<String,String> kvParam = new KvMap<String,String>();
		kvParam.put("precision",TOOLS.intToString(this.mType.getPercesion()));
		String sScale = "0";
		if(this.mType.getScale()>0){
			sScale = TOOLS.intToString(this.mType.getScale());
		}
		kvParam.put("scale",sScale);
		
		try {
			SqlResult sr = Sql2dbInfo.getRuleInfo(this.mType.getTypeName(), 
												  dbType,
					                              TOOLS.intToString(this.mType.getPercesion()),
					                              sScale);
			if(sr!=null && sr.size()>0){
				sRule = sr.get(0, "rule");;
			}
			sRule = TOOLS.paramReaplace(sRule,"precision,scale", kvParam, "&", ",");
			
		} catch (Exception e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e);
			throw mErrorMessage;
		}
		
		return sRule;
	}
	
}
